import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:geomancy/models/base_model.dart';
import 'package:geomancy/tools/constant.dart';
import 'package:geomancy/tools/localstorage.dart';

class UserProvider extends ChangeNotifier {
  String? _token;
  User? _user;
  ///记录选择的生日对应的属相
  String? _zodiac = 'long';

  String? get getToken => _token;
  User? get getUserInfo => _user;
  String? get getZodiac => _zodiac;

  set setNickname(String? value) {
    _user?.username = value;
    notifyListeners();
  }
  set setSex(String? value) {
    _user?.sex = value;
    notifyListeners();
  }
  set setBirthday(String? value) {
    _user?.birthday = value;
    notifyListeners();
  }
  set setZodiac(String? value) {
    _zodiac = value;
    notifyListeners();
  }

  /// 保存用户Token
  Future saveToken(String value) async {
    _token = value;
    final prefs = await LocalStorage.getInstance();
    prefs.setString(Constant.TOKEN, value);
    notifyListeners();
  }
  /// 保存用户信息
  Future saveUserInfo(User u) async {
    _user = u;
    final prefs = await LocalStorage.getInstance();
    String tmp = jsonEncode(u.toJson());
    prefs.setString(Constant.USER, tmp);
    notifyListeners();
  }
  /// 清空
  void clearStore() {
    _token = null;
    _user = null;
    notifyListeners();
  }
}